import org.activiti.engine.*;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricProcessInstanceQuery;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class TestActiviti {
    ApplicationContext ioc=new ClassPathXmlApplicationContext("spring/spring-*.xml");
    ProcessEngine processEngine = (ProcessEngine) ioc.getBean("processEngine");

    @Test
    public void test06(){
        HistoryService historyService = processEngine.getHistoryService();
        HistoricProcessInstanceQuery historicProcessInstanceQuery = historyService.createHistoricProcessInstanceQuery();
        HistoricProcessInstance historicProcessInstance = historicProcessInstanceQuery.processInstanceId("301").finished().singleResult();
        System.out.println("historicProcessInstance="+historicProcessInstance);
    }


    @Test
    public void test05(){
        TaskService taskService = processEngine.getTaskService();
        TaskQuery taskQuery = taskService.createTaskQuery();
        List<Task> list = taskQuery.taskAssignee("zhangsan").list();
        List<Task> list1 = taskQuery.taskAssignee("lisi").list();
        //张三的任务
        System.out.println("张三的任务：");
        for (Task task: list) {
            System.out.println("id="+task.getId());
            System.out.println("name="+task.getName());
            //张三完成任务
            //taskService.complete(task.getId());
        }
        //lisi的任务
        System.out.println("lisi的任务：");
        for (Task task: list1) {
            System.out.println("id="+task.getId());
            System.out.println("name="+task.getName());
            taskService.complete(task.getId());
        }
        System.out.println("--------------------------------------");
        list = taskQuery.taskAssignee("zhangsan").list();
        list1 = taskQuery.taskAssignee("lisi").list();
        //张三的任务
        System.out.println("张三的任务：");
        for (Task task: list) {
            System.out.println("id="+task.getId());
            System.out.println("name="+task.getName());
        }
        //lisi的任务
        System.out.println("lisi的任务：");
        for (Task task: list1) {
            System.out.println("id="+task.getId());
            System.out.println("name="+task.getName());
        }
    }

    //4、创建流程实例
    /**
     * act_hi_actinst,历史的活动的任务表
     * act_hi_procinst，历史的流程实例表
     * act_hi_taskinst,历史的流程任务表
     * act_ru_execution,正在运行的任务表
     * act_ru_task,运行的任务数据表
     */
    @Test
    public void test04(){
        ProcessDefinition processDefinition = processEngine.getRepositoryService().createProcessDefinitionQuery().latestVersion().singleResult();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
        System.out.println("processInstance="+processInstance);
    }
    //3、查询部署的流程定义
    @Test
    public void test03(){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        List<ProcessDefinition> list = processDefinitionQuery.list();
        for (ProcessDefinition processDefinition : list) {
            System.out.println("Id="+processDefinition.getId());
            System.out.println("Key="+processDefinition.getKey());
            System.out.println("Name="+processDefinition.getName());
            System.out.println("Version="+processDefinition.getVersion());
            System.out.println("-----------------------------------");
        }
        long count = processDefinitionQuery.count();//查询流程定义记录条数
        System.out.println("count="+count);
        System.out.println("------------------------------------");
        //查询最后一次流程定义
        ProcessDefinition processDefinition = processDefinitionQuery.latestVersion().singleResult();
        System.out.println("Id="+processDefinition.getId());
        System.out.println("Key="+processDefinition.getKey());
        System.out.println("Name="+processDefinition.getName());
        System.out.println("Version="+processDefinition.getVersion());
        System.out.println("------------------------------------");
        //分页排序查询流程定义
        ProcessDefinitionQuery processDefinitionQuery1 = processDefinitionQuery.orderByProcessDefinitionVersion().desc();
        List<ProcessDefinition> processDefinitions = processDefinitionQuery1.listPage(0, 2);
        for (ProcessDefinition processDefinition1 :
                processDefinitions) {
            System.out.println("Id="+processDefinition1.getId());
            System.out.println("Key="+processDefinition1.getKey());
            System.out.println("Name="+processDefinition1.getName());
            System.out.println("Version="+processDefinition1.getVersion());
            System.out.println("-----------------------------------");
        }
        System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
        //根据流程定义的key对象去查询
        ProcessDefinition myProcess_1 = processDefinitionQuery.processDefinitionKey("myProcess_1").latestVersion().singleResult();
        System.out.println("Id="+myProcess_1.getId());
        System.out.println("Key="+myProcess_1.getKey());
        System.out.println("Name="+myProcess_1.getName());
        System.out.println("Version="+myProcess_1.getVersion());

    }
    //2、部署流程定义
    @Test
    public void test02(){
        System.out.println("processEngine:"+processEngine);
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment().addClasspathResource("Myprocess2.bpmn").deploy();
        System.out.println("deploy:"+deploy);
    }
    //1、创建流程引擎，创建23张表
    @Test
    public void test01(){
        System.out.println("processEngine:"+processEngine);
    }
}
